package com.magicsoft.share.adapter.exchange;

import android.support.annotation.LayoutRes;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.magicsoft.share.R;
import com.magicsoft.share.bean.exchange.ShopCartGoods;

/**
 * Created by lyf on 2017/11/9/009.
 */

public class ShopCartAdapter extends BaseQuickAdapter<ShopCartGoods.ListBean, BaseViewHolder> {


    private boolean isAllcheck = false;

    public ShopCartAdapter(@LayoutRes int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShopCartGoods.ListBean item) {
        helper.setText(R.id.tv_name, item.getGoodsName())
                .setText(R.id.tv_price, item.getPrice() + "")
                .setText(R.id.tv_num, "x" + item.getGoodsNum());
        ImageView imageView = helper.getView(R.id.imageView);
        ImageView checkbox = helper.getView(R.id.checkbox);
        Glide.with(mContext).load(item.getGoodsImg()).into(imageView);
        helper.addOnClickListener(R.id.delete);
        helper.addOnClickListener(R.id.checkbox);
        if (item.getChoice() == 1) {
            Glide.with(mContext).load(R.drawable.sel_check).into(checkbox);
        } else {
            Glide.with(mContext).load(R.drawable.sel_nor).into(checkbox);
        }

        if (item.getStatus()==2){
            helper.setVisible(R.id.tv_no_spec, false);
            if (item.getCheckStock() >= 0) {
                helper.setVisible(R.id.tv_no_spec, false);
            } else {
                helper.setText(R.id.tv_no_spec,"库存不足，请重新加入购物车");
                helper.setVisible(R.id.tv_no_spec, true);
            }
        }else {
            helper.setText(R.id.tv_no_spec,"商品已下架");
            helper.setVisible(R.id.tv_no_spec, true);
        }
    }


    public int setCheck(int position) {
        int money = 0;
        Log.d("lyf", "setCheck: " + position + "," + getItem(position).getCheckStock());
        if (getItem(position).getChoice() == 0) {
            getItem(position).setChoice(1);
        } else {
            getItem(position).setChoice(0);
        }

        notifyItemChanged(position);
        for (int i = 0; i < getData().size(); i++) {
            ShopCartGoods.ListBean listBean = getData().get(i);
            if (listBean.getChoice() == 1) {
                money = money + listBean.getPrice() * listBean.getGoodsNum();
            }

        }

        return money;
    }

    public int setCheckAll() {
        int money = 0;
        if (!isAllcheck) {
            for (int i = 0; i < getData().size(); i++) {
                ShopCartGoods.ListBean listBean = getData().get(i);
                listBean.setChoice(1);
                money = money + listBean.getPrice() * listBean.getGoodsNum();
            }
            isAllcheck = true;
        } else {
            for (int i = 0; i < getData().size(); i++) {
                getData().get(i).setChoice(0);
            }
            isAllcheck = false;
        }
        notifyDataSetChanged();

        return money;
    }


}
